package com.boyon.system.action;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boyon.common.domain.UserSession;
import com.boyon.common.exection.BusinessException;
import com.boyon.common.status.AccountType;
import com.boyon.common.status.Constant;
import com.boyon.common.status.RoleType;
import com.boyon.common.status.StatusType;
import com.boyon.hbm.criteria.OperationType;
import com.boyon.hbm.criteria.Param;
import com.boyon.hbm.criteria.Query;
import com.boyon.system.entity.Menu;
import com.boyon.system.entity.Role;
import com.boyon.system.service.RoleService;
import com.boyon.web.param.annotation.ValidatorRequestBody;
import com.boyon.web.response.PageResponse;
import com.boyon.web.response.SuccessResponse;

/**
 * ClassName:RoleAction <br/>
 * Function: 角色请求类. <br/>
 * Date: 2018年3月15日 上午9:38:22 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Controller
@RequestMapping(value = "/boyon/role", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RoleAction {

	@Autowired
	private RoleService roleService;

	/**
	 * 
	 * editorRole:编辑角色信息. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/editor")
	@ResponseBody
	public SuccessResponse editorRole(@ValidatorRequestBody Role role,
			HttpSession session) {
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		AccountType accountType = AccountType
				.getType(userSession.getAccountType());
		if (role.getRoleId() != null) {
			roleService.modifyRole(role, accountType, userSession);
		} else {
			roleService.createRole(role, accountType, userSession);
		}
		return new SuccessResponse();
	}

	/**
	 * 
	 * findRole:角色ID查找角色信息. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/find")
	@ResponseBody
	public SuccessResponse findRole(@ValidatorRequestBody Role role) {
		return new SuccessResponse(roleService.findByRoleId(role.getRoleId()));
	}

	/**
	 * 
	 * deleteRole:角色ID删除角色信息. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public SuccessResponse deleteRole(@ValidatorRequestBody Role role) {
		roleService.deleteRole(role.getRoleId());
		return new SuccessResponse();
	}

	/**
	 * 
	 * findAuthority:查找角色菜单. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/find/authority")
	@ResponseBody
	public Menu findAuthority(@ValidatorRequestBody Role role,
			HttpSession session) {
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		Integer accountType = userSession.getAccountType();
		Menu menu = roleService.findMenuByRoleId(userSession.getRoleId(),
				role.getRoleId(), accountType);
		return menu;
	}

	/**
	 * 
	 * findManagerMenu:查找管理角色可配置菜单. <br/>
	 * 
	 * @author sunshulin
	 * @param role
	 * @param session
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/find/menu")
	@ResponseBody
	public Menu findManagerMenu(@ValidatorRequestBody Role role,
			HttpSession session) {
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		Integer accountType = userSession.getAccountType();
		if (AccountType.system != AccountType.getType(accountType)) {
			throw BusinessException.builder(0, "只有系统角色可使用管理权限菜单");
		}
		Menu menu = roleService.findMenuByManager(userSession.getRoleId(),
				role.getRoleId(), accountType);
		return menu;
	}

	/**
	 * 
	 * query:角色列表查询. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/query")
	@ResponseBody
	public PageResponse query(@ValidatorRequestBody Role role,
			HttpSession session) {
		Query query = new Query(role.page());
		query.addParam(new Param<String>("roleName", OperationType.like,
				role.getRoleName()));
		query.addParam(new Param<Integer>("roleType", OperationType.eq,
				role.getRoleType()));
		query.addParam(new Param<Integer>("status", OperationType.eq,
				role.getStatus()));
		query.addParam(new Param<Integer>("status", OperationType.nq,
				StatusType.del.getStatus()));
		query.addParam(new Param<Integer>("roleType", OperationType.nq,
				RoleType.system.getVal()));
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		query.addParam(new Param<Long>("relationId", OperationType.eq,
				userSession.getAccountId()));
		return new PageResponse(roleService.query(query), query.getTotal());
	}
}
